// 路由鉴权
// 引入路由
import router from "@/router";
// 引入进度条
// @ts-ignore
import NProgress from "nprogress";
// 引入进度条的样式
import "nprogress/nprogress.css";
import pinia from "@/store"
// @ts-ignore
import useUserStore from "@/store/modules/user";
let userStore = useUserStore(pinia);
// 存储用户未登录可以访问的路由的路径
let whiteList = [
  "/home",
  "/hospital/register",
  "/hospital/detail",
  "/hospital/notice",
  "/hospital/close",
  "/hospital/search",
];
// 设置无加载小圆圈
NProgress.configure({ showSpinner: false });
// 添加相应的全局守卫
// 全局的前置守卫
router.beforeEach((to, from, next) => {
  // 访问路由组件之前，进度条开始动
  // 动态设置网页左上角的标题
  document.title = `大一医院${to.meta.title}`;
  NProgress.start();
  // 判断用户是否登录-token
  let token = userStore.userInfo.token;
  if (token) {
    // 用户登录了
    next();
  } else {
    // 用户未登录
    if (whiteList.includes(to.path)) {
      next();
    } else {
      // 登录组件显示
      userStore.visible = true;
      // 跳转至首页
      next({ path: "/home",query:{redirect:to.fullPath} });
    }
  }
});
// 后置路由
router.afterEach((to, from) => {
  // 访问路由组件成功，进度条消失
  NProgress.done();
});
